package main

import (
	"context"
	"flag"
	"gitee.com/byx_darwin/geo-rpc/internal/base/conf"
	"gitee.com/byx_darwin/micro-extend/pkg/configuration/polaris"
	"gitee.com/byx_darwin/uptrace-opentelemetry/log"
	"gitee.com/byx_darwin/uptrace-opentelemetry/otelplay"
	"github.com/cloudwego/kitex/pkg/klog"
	"gopkg.in/yaml.v3"
)

var (
	namespace = "edge"                    //空间名称
	fileGroup = "pcdn"                    //分组名称
	fileName  = "rpc/jobPlan/config.yaml" //文件名称
)

func main() {
	flag.Parse()
	config := new(conf.Config)
	ctx := context.Background()
	if conf.GetEnv() == "dev" {
		// 解析本地配置
		config = conf.GetConf()
	} else {
		// 解析远程配置文件
		configFile := polaris.ConfigApi(namespace, fileGroup, fileName)
		err := yaml.Unmarshal([]byte(configFile.GetContent()), &config)
		if err != nil {
			klog.CtxFatalf(ctx, "配置文件 yaml反序列化失败 error：%v", err)
		}
	}
	// 自定义日志配置
	if config.Server.Jaeger.Enable {
		shutdown := otelplay.ConfigureOpentelemetry(ctx, &otelplay.Config{
			ServiceDSN:     config.Server.Jaeger.Endpoint,
			ServiceName:    config.Service.Name,
			ServiceVersion: config.Service.Version,
			Environment:    config.Service.Env,
		})
		defer shutdown()
	}
	// 自定义日志配置
	log.NewKitexLog(config.Logger)
	// wire 依赖注入
	svr, cleanup, err := initApp(klog.DefaultLogger(), config)
	if err != nil {
		klog.CtxFatalf(ctx, "initApp error: %v", err)
	}

	defer cleanup()
	if err := svr.Run(); err != nil {
		klog.CtxFatalf(ctx, "run server error: %v", err)
	}
}
